package shortlink.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.Get;
import org.springframework.web.bind.annotation.*;
import shortlink.common.convention.result.Result;
import shortlink.common.convention.result.Results;
import shortlink.dto.req.ShortLinkBatchCreateReqDTO;
import shortlink.dto.req.ShortLinkCreateReqDTO;
import shortlink.dto.req.ShortLinkPageReqDTO;
import shortlink.dto.req.ShortLinkUpdateReqDTO;
import shortlink.dto.resp.ShortLinkBatchCreateRespDTO;
import shortlink.dto.resp.ShortLinkCreateRespDTO;
import shortlink.dto.resp.ShortLinkGroupCountQueryRespDTO;
import shortlink.dto.resp.ShortLinkPageRespDTO;
import shortlink.service.ShortLinkService;

import java.io.IOException;
import java.util.List;
@Slf4j
@RestController
@RequiredArgsConstructor
public class ShortLinkController {
    private final ShortLinkService shortLinkService;

    /*创建短链接*/
    @PostMapping("/api/short-link/project/link/create")
    public Result<ShortLinkCreateRespDTO> craeteShortLink(@RequestBody ShortLinkCreateReqDTO requestParam) {
        return Results.success(shortLinkService.createShortLink(requestParam));
    }
    /*批量创建短链接*/
    @PostMapping("/api/short-link/project/link/Batchcreate")
    public Result<ShortLinkBatchCreateRespDTO> batchcraeteShortLink(@RequestBody ShortLinkBatchCreateReqDTO requestParam) {
        return Results.success(shortLinkService.batchcreateShortLink(requestParam));
    }
    /*修改短链接*/
    @PostMapping("/api/short-link/project/link/update")
    public Result<Void> updateShortLink(@RequestBody ShortLinkUpdateReqDTO requestParam) {
        shortLinkService.updateShortLink(requestParam);
        return Results.success();
    }
    /*分页查询短链接*/
    @GetMapping("/api/short-link/project/link/pageselect")
    public  Result<IPage<ShortLinkPageRespDTO>> pageshortlink( ShortLinkPageReqDTO requestParam) {
        return  Results.success(shortLinkService.pageshortlink(requestParam));
    }
    /*查询短链接分组内数量*/
    @GetMapping("/api/short-link/project/link/groupselect")
    public Result<List<ShortLinkGroupCountQueryRespDTO>> groupshortlink(@RequestParam("requestParam") List<String> requestParam) {
       return Results.success(shortLinkService.groupshortlink(requestParam));
    }
    /*短链接跳转*/
    @GetMapping("/{short-uri}")
    public void restoreUrl(@PathVariable("short-uri") String shortUri, ServletRequest request, ServletResponse response) throws IOException {
        shortLinkService.restoreUrl(shortUri,request,response);
    }


}
